<?php
namespace app\api\controller\deerhome\worker;

use app\api\controller\deerhome\worker\BaseAuth;
use think\Db;
use think\Validate;

/**
 * 用户
 */
class User extends BaseAuth
{
    
    public function index()
    {
        $data=[];
        $data['face']=$this->fixImg($this->_user['face_image']);
        $data['uname']=$this->_user['uname'];
        $data['utel']=\substr_replace($this->_user['utel'],'****',3,4);
        $data['has_card']=0;
        $data['msg']=Db::name("deerhome_msg")->where("is_read",2)->where("utype",2)->where("uid",$this->_user['id'])->count();
        $data['rz']=1;
        $data['lev']="";
        $has=Db::name("deerhome_worker_card")->where("jz_worker_id",$this->_user['id'])->find();
        if($has){
            $data['has_card']=1;
        }
        $lev=Db::name("deerhome_worker_lev")->where("id",$this->_user['jz_worker_lev_id'])->find();
        if($lev){
            $data['lev']=$lev['name']. ($lev['fybl']>0?" +".$lev['fybl']."%":"");
        }
        $this->ok($data);
    }
    public function profile(){
        $data=[];
        $data['face']=$this->fixImg($this->_user['face_image']);
        $data['uname']=$this->_user['uname'];
        $data['utel']=$this->_user['utel'];
        $data['lev']="";
        $data['regtime']=$this->_user['addtime'];
        $data['address']=$this->_user['address'];
        $data['info']=$this->_user['info'];
        $lev=Db::name("deerhome_worker_lev")->where("id",$this->_user['jz_worker_lev_id'])->find();
        if($lev){
            $data['lev']=$lev['name']. ($lev['fybl']>0?" +".$lev['fybl']."%":"");
        }
        $this->ok($data);
    }
    public function set_name(){
        $uname=$this->request->param("uname","",'trim');
        if($uname==""){
            $this->err("姓名不能为空");
        }
        Db::name("deerhome_worker")->where("id",$this->_user['id'])->update(["uname"=>$uname]);
        $this->ok();
    }
    
    public function set_user_info(){
        $user_info=$this->request->param("user_info","",'trim');
        if($user_info==""){
            $this->err("内容不能为空");
        }
        Db::name("deerhome_worker")->where("id",$this->_user['id'])->update(["info"=>$user_info]);
        $this->ok();
    }
    public function set_user_address(){
        $address=$this->request->param("address","",'trim');
        $latitude=$this->request->param("latitude","",'trim');
        $longitude=$this->request->param("longitude","",'trim');
        if($address=="" || $latitude=="" || $longitude==""){
            $this->err("请选择地址");
        }
        Db::name("deerhome_worker")->where("id",$this->_user['id'])->update([
            "address"=>$address,
            "latitude"=>$latitude,
            "longitude"=>$longitude
        ]);
        $this->ok();
    }
    public function set_face(){
        $face=$this->request->param("face","",'trim');
        if($face==""){
            $this->err("请上传头像");
        }
        Db::name("deerhome_worker")->where("id",$this->_user['id'])->update(["face_image"=>$face]);
        $this->ok();
    }
}
